package org.bmind.datasword.config.impl.orient;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
import org.bmind.datasword.utils.StorageUtilities;

public class OrientUtilities {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void oLazyToEager(Object obj) throws Exception {
		if (obj != null) {

			// if
			// (obj.getClass().getName().equalsIgnoreCase("com.orientechnologies.orient.core.db.record.ORecordElement$STATUS")){
			//
			// }

			if (obj instanceof Collection) {
				for (Object o : (Collection<?>) obj)
					oLazyToEager(o);
			}
			if (obj.getClass().isArray()) {
				oLazyToEager(Arrays.asList(obj));
			} else if (obj instanceof Map) {
				oLazyToEager(((Map<?, ?>) obj).keySet());
				oLazyToEager(((Map<?, ?>) obj).values());
			} else if (StorageUtilities.isREngineType(obj)) {
				for (Method m : obj.getClass().getMethods()) {
					if (m.getName().startsWith("get")&& m.getParameterTypes().length == 0) {
						Object propertyValue = m.invoke(obj);
						if (propertyValue != null && propertyValue instanceof Collection)
							obj.getClass().getMethod('s' + m.getName().substring(1),Collection.class).invoke(obj,propertyValue = new LinkedList((Collection) propertyValue));
						oLazyToEager(propertyValue);
					}
				}
			}
		}
	}
	
	
}
